package com.luhui.codereview.swing

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.table.JBTable
import com.luhui.codereview.api.ApiManager
import com.luhui.codereview.getMessage
import com.luhui.codereview.service.ProjectService
import com.luhui.codereview.token.LocalUserInfoHodler
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.awt.EventQueue
import javax.swing.JTable
import javax.swing.table.DefaultTableModel

/**
 * @author luhui
 * @version 1.0
 * @date 2019/11/18 22:13
 */
class UserListTabTable(
        val project: Project
) : JBTable(){

    init {
        this.autoResizeMode = JTable.AUTO_RESIZE_OFF
    }

    override fun isCellEditable(row: Int, column: Int): Boolean {
        return false
    }

    abstract class BaseUserListTable(userListTabTable:UserListTabTable) : JBScrollPane(userListTabTable){

        /**
         * 刷新列表
         */
        abstract fun refresh()

    }

    private inner class UserListTab : BaseUserListTable(this@UserListTabTable){
        init{
            loadUserListTableData(false)
        }

        override fun refresh() {
            loadUserListTableData(true)
        }
    }


    companion object {

        fun buildUserListTab(project: Project) : BaseUserListTable{
            val table = UserListTabTable(project)
            return table.UserListTab()
        }
    }

    /**
     * 加载用户列表表格数据
     */
    private fun loadUserListTableData(showError:Boolean) {
        val userId = LocalUserInfoHodler.localUserInfo?.id
        if(userId==null){
            if(showError) {
                Messages.showErrorDialog(getMessage("scanQrCode.userNotAuth.msg"), getMessage("notFoundProjectId.title"))
            }
            return
        }
        val projectId = ProjectService.getInstance(project).getCurrentProjectInfo()?.id
        if(projectId==null){
            EventQueue.invokeLater{
                if(showError) {
                    Messages.showErrorDialog(getMessage("notFoundProjectId.msg"), getMessage("notFoundProjectId.title"))
                }
            }
            return
        }

        GlobalScope.launch {
            val userList = ApiManager.getApiInstance().getUserList(projectId)
            EventQueue.invokeLater {
                //加载实时数据
                val model = initUserListModel()

                for (data in userList) {
                    model.addRow(arrayOf(data.realName,null,null))
                }
            }
        }
    }

    fun initUserListModel(): DefaultTableModel {
        val model = DefaultTableModel()
        model.setColumnIdentifiers(arrayOf(getMessage("tab.table.userList.userName"), getMessage("tab.table.userList.joinTime"), getMessage("tab.table.userList.role")))
        this.model = model
        this.columnModel.getColumn(0).preferredWidth = 50
        this.columnModel.getColumn(1).preferredWidth = 50
        this.columnModel.getColumn(2).preferredWidth = 50
        return model
    }

}
